<template>
  <div class="leads-container">
    <div class="main-card">
      <!-- 页面标题 -->
      <div class="card-header">
        <h2 class="card-title">线索管理</h2>
      </div>
      <div class="search-section">
        <div class="search-container">
          <div class="search-item">
            <span class="search-label">类型</span>
            <el-select v-model="queryParams.type" placeholder="请选择类型" clearable class="search-input" @change="handleQuery">
              <el-option :value="1" label="软著" />
              <el-option :value="2" label="作业、毕设" />
            </el-select>
          </div>
          <div class="search-buttons">
            <el-button type="primary" @click="handleQuery" :icon="Search">查询</el-button>
            <el-button @click="resetQuery" :icon="RefreshRight">重置</el-button>
          </div>
        </div>
      </div>

      <!-- 表格区域 -->
      <div class="table-section">
        <el-table :data="leadList" fit v-loading="loading" border stripe highlight-current-row
          :cell-style="{ textAlign: 'center' }">
          <el-table-column prop="id" label="ID" align="center" width="80" />
          <el-table-column prop="weChat" label="微信号" align="center" />
          <el-table-column label="类型" align="center">
            <template #default="{ row }">
              <el-tag :type="row.type === 1 ? 'success' : 'warning'" 
                      :effect="row.type === 1 ? 'light' : 'light'">
                {{ row.type === 1 ? '软著' : '作业、毕设' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="updateTime" label="更新时间" align="center" />
          <el-table-column label="操作" align="center" width="120" fixed="right">
            <template #default="{ row }">
              <el-button type="danger" link @click="handleDelete(row)" class="action-button">
                <el-icon>
                  <Delete />
                </el-icon>
                <span>删除</span>
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <div class="pagination-container">
          <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize"
            :page-sizes="[10, 20, 50, 100]" background layout="total, sizes, prev, pager, next, jumper" :total="total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { getServicePageList, deleteServiceById } from '@/api/service'
import { debounce } from 'lodash-es'
import { Search, RefreshRight, Delete } from '@element-plus/icons-vue'

// 查询参数
const queryParams = reactive({
  type: null,
  pageNum: 1,
  pageSize: 10
})

// 数据列表
const leadList = ref([])
const total = ref(0)
const loading = ref(false)

// 防抖处理查询
const debouncedQuery = debounce(() => {
  getLeadList()
}, 300)

// 获取线索列表数据
const getLeadList = async () => {
  if (loading.value) return
  loading.value = true
  try {
    const params = {
      type: queryParams.type,
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize
    }
    const res = await getServicePageList(params)
    if (res.code === 200) {
      leadList.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error({
        message: res.msg || '获取线索列表失败',
        duration: 3000,
        showClose: true
      })
    }
  } catch (error) {
    console.error('获取线索列表出错:', error)
    // 403错误已在请求拦截器中处理，这里不再显示错误消息
    if (!error.code || error.code !== 403) {
      ElMessage.error({
        message: '获取线索列表出错，请稍后重试',
        duration: 3000,
        showClose: true
      })
    }
  } finally {
    loading.value = false
  }
}

// 查询按钮
const handleQuery = () => {
  queryParams.pageNum = 1
  debouncedQuery()
}

// 重置按钮
const resetQuery = () => {
  queryParams.type = null
  handleQuery()
}

// 分页大小变化
const handleSizeChange = (size) => {
  queryParams.pageSize = size
  debouncedQuery()
}

// 页码变化
const handleCurrentChange = (current) => {
  queryParams.pageNum = current
  debouncedQuery()
}

// 删除线索
const handleDelete = async (row) => {
  ElMessageBox.confirm(
    `确定要删除微信号为「${row.weChat}」的线索吗？此操作不可逆。`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
    const loading = ElLoading.service({
      lock: true,
      text: '正在删除...',
      background: 'rgba(0, 0, 0, 0.7)'
    })

    try {
      const res = await deleteServiceById(row.id)
      if (res.code === 200) {
        ElMessage.success('删除成功')
        getLeadList() // 刷新列表
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    } catch (error) {
      // 403错误已在请求拦截器中处理，这里不再显示错误消息
      if (!error.code || error.code !== 403) {
        ElMessage.error('删除失败，请稍后重试')
      }
    } finally {
      loading.close()
    }
  }).catch(() => {
    // 用户取消操作
  })
}

// 页面加载时获取数据
onMounted(() => {
  getLeadList()
})
</script>

<style scoped>
.leads-container {
  min-height: calc(100vh - 120px);
  background-color: #f5f7fa;
}

.main-card {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
}

.card-title {
  font-size: 18px;
  font-weight: 500;
  color: #303133;
  margin: 0;
  line-height: 1.5;
}

.search-section {
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
}

.search-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.search-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.search-label {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

.search-input {
  width: 220px;
}

.search-buttons {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.table-section {
  padding: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.action-button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
}

.action-button .el-icon {
  font-size: 16px;
}

/* 响应式设计 */
@media screen and (max-width: 768px) {
  .leads-container {
    padding: 10px;
  }

  .search-container {
    flex-direction: column;
    align-items: flex-start;
  }

  .search-item {
    width: 100%;
  }

  .search-input {
    width: 100%;
  }

  .search-buttons {
    width: 100%;
    margin-top: 15px;
  }
}

:deep(.el-tag) {
  margin: 2px;
  font-size: 13px;
  padding: 0 10px;
  height: 28px;
  line-height: 26px;
}
</style>